package com.naiking.manager
{
	import com.naiking.data.HashMap;
	import com.naiking.interfaces.IPoolItem;
	
	import flash.utils.getDefinitionByName;

	/**
	 * ObjectPoolManager
	 * @Nai.King
	 * 
	 */	
	public class ObjectPoolManager
	{
		//dictionary
		private var pools:HashMap=new HashMap();
		//存取各个池的配置 name: maxLen:
		private var _poolsConfig:HashMap=new HashMap();
		/**
		 * 用之前务必要把类引入 编译进Flash 如：Ball;
		 * 类必须要继承Ipool接口
		 * @param classType:ClassName类的全名称 String
		 * @param maxSize
		 */		
		public function initPool(classFullName:String,maxSize:int):void
		{
			//create all item by once
			trace("Created pool of "+classFullName);
			if(pools.containsKey(classFullName))
				return;
			var itemAry:Array=[];
			pools.add(classFullName,itemAry);
			_poolsConfig.add(classFullName,maxSize);
			var cls:Class=getDefinitionByName(classFullName) as Class;
			for(var i:int=0;i<maxSize;i++)
			{
				itemAry.push(new cls());
				trace("create......"+i);
			}
		}
		
		public function getPoolMaxLength(poolName:String):int
		{
			if(_poolsConfig.containsKey(poolName))
			{
				return _poolsConfig.getValue(poolName)
			}
			return 0;
		}
		public function getPoolCntLength(clsName:String):int
		{
			if(pools.containsKey(clsName))
			{
				return (pools.getValue(clsName) as Array).length;
			}
			return 0;
		}
		/**只负责reset 不负责removeChild与EventListener*/
		public function returnItem(classFullName:String,item:IPoolItem):void
		{
			if(!pools.containsKey(classFullName))
			{
				throw new Error("Not find："+classFullName+" pool");
				return;
			}
			trace("Give back item："+classFullName);
			if(getPoolCntLength(classFullName)>=getPoolMaxLength(classFullName))
			{
				//对于超出的直接销毁吧
				trace("直接销毁----");
				item.dispose();
			}else
			{
				trace("直接归还===");
				item.reset();
				(pools.getValue(classFullName) as Array).push(item);
			}
			
		}
		/**
		 *Get one item from pool 
		 * @param classType：
		 * @return 
		 * 
		 */		
		public function borrowItem(classType:String):IPoolItem
		{
			if(!pools.containsKey(classType))
			{
				throw new Error("Not find："+classType+" pool");
				//return;
			}
			if((pools.getValue(classType) as Array).length==0)
			{
				var cls:Class=getDefinitionByName(classType) as Class;
				var tempItem:IPoolItem=new cls() as IPoolItem;
				(pools.getValue(classType) as Array).push(tempItem);
				trace("超出了界限 临时创建");
				//throw new Error("The Pool："+classType+" is full，Need expand");
				//return;
			}
			trace("Borrowed a item of:"+classType);
			return (pools.getValue(classType) as Array).pop();
		}
		public function disposePool(classType:String):void
		{
			if(!pools.containsKey(classType))
				return;
			//clear pool
			var itemAry:Array=pools.getValue(classType) as Array;
			//dispose objects in pool
			for(var i:int=0;i<itemAry.length;i++)
			{
				/*
				the dispose() function is used to 
				removeEventListener、dispose Bitmapdata、delete dynamic property 
				*/
				(itemAry[i] as IPoolItem).dispose();
			}
			itemAry=null;
			//remove this key from dictionary
			pools.remove(classType);
		}
		public function ObjectPoolManager(_sig:Sig)
		{
		}
		private  static var _instance:ObjectPoolManager;
		public static function getInstance():ObjectPoolManager
		{
			if(!_instance)
				_instance=new ObjectPoolManager(new Sig());
			return _instance;
		}
	}
}
final class Sig{}